<template>
  <div class="app-container">
    <el-header>
      <el-button type="primary" plain @click="convert">转换</el-button>
    </el-header>
    <el-main>
      <el-row :gutter="24">
        <el-col :span="12">
          实体：
          <div>
            <el-input v-model="entityStr" type="textarea" :rows="40" @blur="convert" /></div
        ></el-col>
        <el-col :span="12">
          JSON：
          <div>
            <el-input v-model="jsonStr" type="textarea" :rows="40" readonly="ture" /></div
        ></el-col>
      </el-row>
    </el-main>
  </div>
</template>

<script>
// import {
//   listUser,
//   getUser,
//   delUser,
//   addUser,
//   updateUser,
//   exportUser,
// } from "@/api/web/user";

export default {
  name: 'EntityToJson',
  components: {},
  data() {
    return {
      entityStr: null,
      jsonStr: null
    }
  },
  created() {},
  methods: {
    convert() {
      this.jsonStr = null
      var strArr = this.entityStr.split(/[\n]/)
      // 获取的string 去空行
      strArr = strArr.filter((item) => {
        return item && item.trim()
      })
      var tempArr = []
      var classStr = ''

      for (var i = 0; i < strArr.length; i++) {
        var j = 1
        var lineStr = this.trimRight(this.trimLeft(strArr[i]))
        var fieldStr = ''

        // 实体名
        if (lineStr.indexOf('public class ') == 0) {
          var className = ''
          if (lineStr.indexOf('extends') >= 0) {
            className = lineStr.match(/class(.*\S*)extends/)[1]
          } else if (lineStr.indexOf('implements') >= 0) {
            className = lineStr.match(/class(.*\S*)implements/)[1]
          } else if (lineStr.indexOf('{') >= 0) {
            className = lineStr.match(/class(.*\S*){/)[1]
          }
          classStr += '"' + className.trim() + '":{\n'
        }

        // 静态资源
        if (lineStr.indexOf('static') >= 0) {
          continue
        }

        // 实体内值
        if (lineStr.indexOf('private') >= 0) {
          var upLineStr = this.trimRight(this.trimLeft(strArr[i - j]))

          // 多种情况兼容
          if (upLineStr.indexOf('@JsonSerialize') >= 0) {
            j++
            upLineStr = this.trimRight(this.trimLeft(strArr[i - j]))
          }

          var attributeName = ''

          if (upLineStr.indexOf('@RdColumn') >= 0) {
            upLineStr = upLineStr.replace(/\s+/g, '')
            // attributeName = upLineStr.match(/title \= \"([^"]+)\",/)[1];
            attributeName = upLineStr.match(/title\=\"([^"]+)\"/)[1]
          } else if (upLineStr.indexOf('//') >= 0) {
            attributeName = upLineStr.replace('//', '')
          } else if (upLineStr.indexOf('*/') >= 0) {
            j++
            var up2LineStr = this.trimRight(this.trimLeft(strArr[i - j]))
            if (up2LineStr.indexOf('@') > 0) {
              j++
              up2LineStr = this.trimRight(this.trimLeft(strArr[i - j]))
            }
            attributeName = up2LineStr.replace('*', '')
          } else if (upLineStr.indexOf('@Excel') >= 0) {
            upLineStr = upLineStr.replace(/\s+/g, '')
            // attributeName = upLineStr.match(/title \= \"([^"]+)\",/)[1];
            attributeName = upLineStr.match(/name\=\"([^"]+)\"/)[1]
          }
          lineStr = lineStr.replace(';', ' ')
          lineStr = lineStr.replace('=', ' ')
          console.log(lineStr)
          var lineArr = lineStr.split(' ')
          var fieldName = lineArr[2]
          console.log(fieldName + '--' + lineArr.length)
          if (lineStr.indexOf('List') > 0) {
            fieldStr += '"' + fieldName + '":[]' + ',       //' + attributeName
          } else if (lineStr.indexOf('Map') > 0) {
            fieldStr += '"' + fieldName + '":{}' + ',       //' + attributeName
          } else {
            fieldStr += '"' + fieldName + '":null' + ',       //' + attributeName
          }
          if (lineArr[lineArr.length - 1].indexOf('//') > -1) {
            fieldStr += lineArr[lineArr.length - 1]
          }
          tempArr.push(fieldStr)
        }
      }
      tempArr[tempArr.length - 1] = tempArr[tempArr.length - 1].replace(',', '')
      console.log('tempArr.findIndex(-1)')
      console.log(tempArr[tempArr.length - 1])
      this.jsonStr = classStr + tempArr.join('\n') + (classStr != '' ? '\n}' : '')
    },
    // 去掉左边的空白
    trimLeft(s) {
      if (s == null) {
        return ''
      }
      var whitespace = new String(' \t\n\r')
      var str = new String(s)
      if (whitespace.indexOf(str.charAt(0)) != -1) {
        var j = 0
        var i = str.length
        while (j < i && whitespace.indexOf(str.charAt(j)) != -1) {
          j++
        }
        str = str.substring(j, i)
      }
      return str
    },
    // 去掉右边的空白
    trimRight(s) {
      if (s == null) return ''
      var whitespace = new String(' \t\n\r')
      var str = new String(s)
      if (whitespace.indexOf(str.charAt(str.length - 1)) != -1) {
        var i = str.length - 1
        while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) {
          i--
        }
        str = str.substring(0, i + 1)
      }
      return str
    }
  }
}
</script>
<style scoped></style>
